import axios from 'axios'
function deleteItem(url,params){
  return new Promise((resolve,reject)=>{
    this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        this.axios
          .delete(url, {
            params:params
          })
          .then((res) => {
            let code = res.data.code;
            if (code == 200) {
              this.$message.success("删除成功");
              resolve(200)
            } else {
              this.$message.error("删除失败");
              resolve(400)
            }
          })
          .catch((err) => {
            this.$message.error("删除失败");
            resolve(400)
          });
      })
      .catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
        resolve(400)
      });
  })
}

function addItem(methods,url,data){
  return new Promise((resolve,reject)=>{
        this.axios({
            method:methods,
            data:data,
            url:url
          })
          .then((res) => {
            let code = res.data.code;
            if (code == 200) {
              this.$message.success("添加成功");
              resolve(200)
            } else {
              this.$message.error("添加失败");
              resolve(400)
            }
          })
          .catch((err) => {
            this.$message.error("添加失败");
            resolve(400)
          });
  })
}

function getInitialItems(method,url,params){
  if(method=='get'){
    return new Promise((resolve,reject)=>{
      this.axios({
        url:url,
        method:method,
        params:params
      }).then(res=>{
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    })
  }
}
export default {deleteItem,addItem,getInitialItems}